﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;

namespace System.Linq
{
    public class WhereAppend<TSource>
    {
        protected List<(string type, Expression<Func<TSource, bool>> predicate)> Wheres = new List<(string, Expression<Func<TSource, bool>>)>();


        public WhereAppend<TSource> Or(Expression<Func<TSource, bool>> predicate)
        {
            Wheres.Add(("Or", predicate));
            return this;
        }
        public WhereAppend<TSource> Or(Expression<Func<TSource, bool>> predicate, bool condition)
        {
            if (condition)
                Wheres.Add(("Or", predicate));
            return this;
        }
        public WhereAppend<TSource> Or(Expression<Func<TSource, bool>> predicate, string condition)
        {
            if (condition != null)
                Wheres.Add(("Or", predicate));
            return this;
        }
        public WhereAppend<TSource> Or(Expression<Func<TSource, bool>> predicate, int condition)
        {
            if (condition > 0)
                Wheres.Add(("Or", predicate));
            return this;
        }
        public WhereAppend<TSource> And(Expression<Func<TSource, bool>> predicate)
        {
            Wheres.Add(("And", predicate));
            return this;
        }
        public WhereAppend<TSource> And(Expression<Func<TSource, bool>> predicate, bool condition)
        {
            if (condition)
                Wheres.Add(("And", predicate));
            return this;
        }
        public WhereAppend<TSource> And(Expression<Func<TSource, bool>> predicate, string condition)
        {
            if (condition != null)
                Wheres.Add(("And", predicate));
            return this;
        }

        public WhereAppend<TSource> And(Expression<Func<TSource, bool>> predicate, int condition)
        {
            if (condition > 0)
                Wheres.Add(("And", predicate));
            return this;
        }

    }
    public class WhereAppendCreate<TSource> : WhereAppend<TSource>
    {
        public IQueryable<TSource> Build(IQueryable<TSource> source)
        {
            if (Wheres.Count == 0)
            {
                return source;
            }
            var parameter = Wheres[0].predicate.Parameters;
            string name = parameter.First().Name;
            Expression body = Wheres[0].predicate.Body;
            for (int i = 1; i < Wheres.Count; i++)
            {
                if (Wheres[i].type == "Or")
                {
                    body = Expression.OrElse(body, Change(Wheres[i].predicate.Body, name));
                }
                else
                {
                    body = Expression.AndAlso(body, Change(Wheres[i].predicate.Body, name));
                }
            }
            var lab = Expression.Lambda<Func<TSource, bool>>(body, parameter);
            return source.Where(lab);
        }

        private Expression Change(Expression expression, string name)
        {//先不转吧，统一语法

            return expression;
        }

    }
}
